﻿using System;
using System.Collections;
using System.Web.UI.WebControls;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.PhieuDieuTraCoSoForm;
using ComputerVision.Services;
using ComputerVision.Services.User;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.NganhForm
{
    public partial class frmNganhDetail : frmDetail
    {
        private readonly IService<Nganh> _nganhService;
        private frmPhieuDieuTraCoSoList _frmPhieuDieuTraCsList;
        private frmPhieuDieuTraCoSoDetail _frmPhieuDieuTraCs;
        private readonly IUserService<aspnet_Users> _userService;
        private readonly IService<ChuyenDe> _chuyenDe;
        

        public frmNganhDetail()
        {
            InitializeComponent();
            _nganhService = Program.Container.Resolve<IService<Nganh>>();
            _chuyenDe = Program.Container.Resolve<IService<ChuyenDe>>();            
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Thêm mới Ngành";
            LoadChuyenDe(null);
            drlChuyenDe.Focus();
        }

        public frmNganhDetail(frmPhieuDieuTraCoSoDetail frm)
        {
            InitializeComponent();
            this._frmPhieuDieuTraCs = frm;
            _nganhService = Program.Container.Resolve<IService<Nganh>>();
            _chuyenDe = Program.Container.Resolve<IService<ChuyenDe>>();         
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Thêm mới Ngành";
            LoadChuyenDe(null);
            drlChuyenDe.Focus();
        }

        public frmNganhDetail(frmPhieuDieuTraCoSoList frm)
        {
            InitializeComponent();
            this._frmPhieuDieuTraCsList = frm;
            _nganhService = Program.Container.Resolve<IService<Nganh>>();
            _chuyenDe = Program.Container.Resolve<IService<ChuyenDe>>();         
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Thêm mới Ngành";
            LoadChuyenDe(null);
            drlChuyenDe.Focus();
        }
        
        public frmNganhDetail(Guid id)
        {
            InitializeComponent();
            _nganhService = Program.Container.Resolve<IService<Nganh>>();
            _chuyenDe = Program.Container.Resolve<IService<ChuyenDe>>();         
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Sửa Ngành";
            
            drlChuyenDe.Focus();

            _fromtype = Constants.TypeForm.Edit;
            guid = id;

            Nganh item = _nganhService.Detail(id);
            if (item != null)
            {
                txtTen.Text = item.Ten;
                txtMa.Text = item.Ma;
                if (item.ChuyenDeId.HasValue)
                {
                    LoadChuyenDe(item.ChuyenDe.Id);
                }
                else
                {
                    LoadChuyenDe(null);
                }
            }
        }

        private void LoadChuyenDe(Guid? id)
        {
            var items = _chuyenDe.ToList();
            drlChuyenDe.Items.Clear();

            var defaultItem = new ListItem();
            defaultItem.Text = string.Empty;
            defaultItem.Value = string.Empty;
            //defaultItem.Selected = true;
            drlChuyenDe.Items.Insert(0, defaultItem);

            int index = 0;
            int i = 0;

            foreach (var chuyenDe in items)
            {
                index++;

                var item = new ListItem();
                item.Text = chuyenDe.Ten;
                item.Value = chuyenDe.Id.ToString("B");

                if (id.HasValue)
                {
                    if (item.Value == id.Value.ToString("B"))
                    {
                        item.Selected = true;
                        i = index;
                    }
                }
                
                drlChuyenDe.Items.Add(item);                          
            }

            drlChuyenDe.SelectedIndex = i;      
        }

        #region Validation

        protected override bool Validator()
        {
            bool flag = true;

            errorProvider.Dispose();

            var selectedItem = drlChuyenDe.SelectedItem as ListItem;
            if (selectedItem == null || string.IsNullOrEmpty(selectedItem.Value))
            {
                errorProvider.SetError(drlChuyenDe, "Bạn chưa chọn chuyên đề. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            //if (string.IsNullOrEmpty(txtMa.Text))
            //{
            //    errorProvider.SetError(txtMa, "Bạn chưa nhập mã ngành. Xin vui lòng kiểm tra lại");
            //    flag = false;
            //}

            if (string.IsNullOrEmpty(txtTen.Text))
            {
                errorProvider.SetError(txtTen, "Bạn chưa nhập tên ngành. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                var chuyendDe = drlChuyenDe.SelectedItem as ListItem;

                var model = new Nganh
                {
                    Id = Guid.NewGuid(),
                    Ten = txtTen.Text,
                    Ma = txtMa.Text,
                    Created = DateTime.Now,
                    CreatedBy_Id = Constants.CurrentUser,
                    Modified = DateTime.Now,
                    ModifiedBy_Id = Constants.CurrentUser,
                    ChuyenDeId = Guid.Parse(chuyendDe.Value)
                };
                _nganhService.Create(model);

                if (this._frmPhieuDieuTraCs != null)
                {
                    this._frmPhieuDieuTraCs.LoadNganh();
                }

                if (this._frmPhieuDieuTraCsList != null)
                {
                    this._frmPhieuDieuTraCsList.LoadNganh();
                }
            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Edit

        protected override void Edit()
        {
            var chuyendDe = drlChuyenDe.SelectedItem as ListItem;

            var item = _nganhService.Detail(guid);
            if (item != null)
            {
                item.Ten = txtTen.Text;
                item.Ma = txtMa.Text;
                item.Modified = DateTime.Now;
                item.ModifiedBy_Id = Constants.CurrentUser;

                item.ChuyenDeId = Guid.Parse(chuyendDe.Value);

                _nganhService.Update(item);
            }
        }

        #endregion
    }
}
